package com.example.birdfriends.controller;

import com.example.birdfriends.pojo.follow.FollowFront;
import com.example.birdfriends.pojo.follow.Follower;
import com.example.birdfriends.pojo.user.User;
import com.example.birdfriends.service.FollowService;
import com.example.birdfriends.service.UserService;
import com.example.birdfriends.utils.convert.ConvertService;
import com.example.birdfriends.utils.result.Result;
import com.example.birdfriends.utils.toSet.ToSetData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/follow")
public class FollowController {
    @Autowired
    private UserService userService = null;
    @Autowired
    private FollowService followService = null;
    @Autowired
    private Result result = null;
    @Autowired
    private ConvertService convertService = null;

    @PostMapping("/addFollow")
    public Map<String, Object> addFollow(@RequestBody FollowFront follow){
        System.out.print("加关注：");follow.printFollowFront();
        // 前端传入关注者的id和被关注者的id
        Long curLoginUid = follow.getCurrentUid();  // 当前登录用户id
        Long followedUid = follow.getFollowedUid();

        Follower savedFollow = new Follower();
        savedFollow.setFollowUid(curLoginUid); savedFollow.setFollowedId(followedUid);
        Timestamp followTime = new Timestamp(System.currentTimeMillis());
        savedFollow.setFollowTime(followTime);

        // 一个用户只能关注一个用户一次
        if(followedUid.equals(curLoginUid)){
            return result.resultMap(false, "不可关注自己", null);
        }

        int add = followService.addFollow(savedFollow);
        boolean success = add > 0;
        FollowFront followFront = convertService.follow2FollowFront(savedFollow, success);
        if(success){
            return result.resultMap(true, "关注成功", followFront);
        }

        return result.resultMap(false, "关注失败", followFront);
    }

    @PostMapping("/cancelFollow")
    public Map<String, Object> cancelFollow(@RequestBody FollowFront followFront){
        System.out.print("取消关注:");followFront.printFollowFront();
        Long fid = followFront.getFid();
        int delete = followService.deleteFollowByFid(fid);
        boolean success = delete > 0;
        if(success){
            // 取关成功
            followFront.setFid(null);
            followFront.setFollow(false);
            return result.resultMap(true, "取关成功", followFront);
        }

        return result.resultMap(false, "取关失败", followFront);
    }

    @Autowired
    private ToSetData setData = null;

    @PostMapping("/myFollow")
    public Map<String, Object> queryMyFollow(@RequestBody User user){
        Long curLoginUid = user.getId();  // 当前登录的用户id
        List<User> myFollowUsers = setData.followUserList(curLoginUid);

        boolean success = myFollowUsers != null && !myFollowUsers.isEmpty();
        if(success){
            return result.resultMap(true, "获取关注列表成功", myFollowUsers);
        }

        return result.resultMap(false, "还没有关注哦", myFollowUsers);
    }

    @PostMapping("isFollow")
    public Map<String, Object> isFollowed(@RequestBody FollowFront followFront){
        Long curLoginUid = followFront.getCurrentUid();
        Long followedUid = followFront.getFollowedUid();
//        followFront.printFollowFront();
        Long fid = followService.findFidByCurUidAndFollowedUid(curLoginUid, followedUid);
        boolean isFollow = fid != null;
        followFront.setFid(fid); followFront.setFollow(isFollow);
        return result.resultMap(true, "判断当前登录用户是否关注用户", followFront);
    }
}
